<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetTuanViewDeta } from '@/api/modules/tuangou'
import GetCacheData from '@/store/modules/NewPageCache'

// const imgDomain = 'https://img1.eprhan.cc'
const route = useRoute()
// const router = useRouter()
const cacheData = GetCacheData()

// 查询表单
const findFrom = reactive({
  ...(cacheData.GetCacheData('tuangouTuanView').fromData),
})

const dataList: any = reactive({
  list: [],
})

let PageNum = 1
const loading = ref(true)
const total = ref(0)

async function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.TuanInfoId && { TuanInfoId: findFrom.TuanInfoId }),
    ...(findFrom.UserId && { UserId: findFrom.UserId }),
  }
  // const parameter = qs.stringify(params)
  await GetTuanViewDeta(params).then(async (dataRes: any) => {
    if (dataRes.succeeded) {
      dataList.list = dataRes.data.items
      total.value = dataRes.data.total
      loading.value = false
    }
    else {
      ElMessage.error(`查询失败。${dataRes.errors}`)
      loading.value = false
    }
  })
    .catch(() => {
      loading.value = false
    })
  loading.value = false
}

// 查询
function Inquire() {
  GetDataList(1, cacheData.GetCacheData('tuangouTuanView').currentSize)
  cacheData.SetCacheData('tuangouTuanView', PageNum, cacheData.GetCacheData('tuangouTuanView').currentSize, findFrom.value)
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  cacheData.SetCacheData('tuangouTuanView', PageNum, val)
}
function handleCurrentChange(val: number) {
  GetDataList(val, cacheData.GetCacheData('tuangouTuanView').currentSize)
  cacheData.SetCacheData('tuangouTuanView', val, cacheData.GetCacheData('tuangouTuanView').currentSize)
  PageNum = val
}

const nowPageNum = computed(() => {
  return cacheData.GetCacheData('tuangouTuanView').currentPage || 1
})

onMounted(async () => {
  if (route.query.tuanLeaderId) {
    findFrom.TuanLeaderId = route.query.tuanLeaderId
    Inquire()
  }
  else {
    GetDataList(PageNum, cacheData.GetCacheData('tuangouTuanView').currentSize)
  }
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="团ID">
                <el-input v-model="findFrom.TuanInfoId" placeholder="请输入团ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <!-- <div class="mb-5 text-right">
        <el-button type="success" @click="AddData">
          添加
        </el-button>
      </div> -->
      <el-table v-loading="loading" :data="dataList.list" stripe style="width: 100%;" border>
        <el-table-column align="center" prop="subscriberInfo.userId" label="用户ID" width="100" />
        <el-table-column align="center" prop="tuanLeaderAvatar" label="用户头像" width="100">
          <template #default="scope">
            <el-avatar shape="square" :size="60" :src="scope.row.subscriberInfo?.userAvatar" />
          </template>
        </el-table-column>
        <el-table-column align="center" prop="subscriberInfo.userNickName" label="用户昵称" min-width="200" />
        <el-table-column align="center" prop="subscriberInfo.userPhone" label="授权手机号" width="300" />
        <el-table-column align="center" prop="subscriberInfo.scOpenId" label="OpenID" width="300" />
        <!-- <el-table-column align="center" prop="subscriberInfo" label="用户信息">
          <template #default="scope">
            <div v-if="!scope.row.subscriberInfo">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriberInfo?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriberInfo?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriberInfo?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column> -->
        <el-table-column align="center" prop="subscriberInfo.unionId" label="unionId" width="300" />
        <el-table-column align="center" width="200" prop="addTime" label="添加时间" />
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="cacheData.GetCacheData('tuangouTuanView').currentSize"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        :default-current-page="nowPageNum"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }

  @media screen and (width <= 500px) {
    .from {
      grid-template-columns: repeat(auto-fill, minmax(30vw, 1fr));
    }
  }
}

.scrollbar-flex-content {
  display: flex;
  max-width: 300px;

  .scrollbar-flex-img {
    display: flex;
    flex-shrink: 0;
    align-items: center;
    justify-content: center;
    width: auto;
    height: 60px;
    margin-right: 10px;
  }
}
</style>
